package com.qumitech.visualnovel.service.magicbox.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.datasource.console.MagicBoxDOMapper;
import com.qumitech.visualnovel.datasource.app.UserPoundBoxStatisticsDOMapper;
import com.qumitech.visualnovel.model.dao.console.MagicBoxDO;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.poundmagicbox.MagicBoxQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.poundmagicbox.UserPoundBoxStatisticAdminQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.poundmagicbox.UserPoundBoxStatisticDTO;
import com.qumitech.visualnovel.service.exception.ExceptionService;
import com.qumitech.visualnovel.service.magicbox.MagicBoxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ywxie
 * @date 2019/12/25 10:15
 * @description
 */
@Service
public class MagicBoxServiceImpl implements MagicBoxService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    private static final String PLAYER_LIST_URL = "/account/admin/information";

    @Autowired
    private MagicBoxDOMapper magicBoxDOMapper;

    @Autowired
    private UserPoundBoxStatisticsDOMapper userPoundBoxStatisticsDOMapper;

    @Autowired
    private ExceptionService exceptionService;

    @Override
    public Integer countMagicBox(MagicBoxQueryDTO queryDTO) {
        return magicBoxDOMapper.countMagicBox(queryDTO);
    }

    @Override
    public List<MagicBoxDO> listMagicBox(MagicBoxQueryDTO queryDTO) {
        JSONArray accountIdArray = new JSONArray();
        List<MagicBoxDO> magicBoxDOList = magicBoxDOMapper.listMagicBox(queryDTO);
        for(MagicBoxDO magicBoxDO: magicBoxDOList){
            accountIdArray.add(magicBoxDO.getAccountId());
        }

        JSONObject postData = new JSONObject();
        postData.put("list_uid", accountIdArray);
        logger.info("postData:" + JSONObject.toJSONString(postData));
        String body = HttpInvokeUtil.httpPostJson(UrlConstant.SERVER_URL + PLAYER_LIST_URL, JSONObject.toJSONString(postData),UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(rsp);
        logger.info("body:" + rsp.toString());
        JSONObject data = (JSONObject)rsp.getData();
        JSONArray accountList = data.getJSONArray("account_list");
        Map<String, String> uidNicknameMap = new HashMap<>();
        for(int i=0;i<accountList.size();i++){
            JSONObject accountInfo = accountList.getJSONObject(i);
            uidNicknameMap.put(accountInfo.getString("uid"), accountInfo.getString("user_name"));
        }
        for(MagicBoxDO magicBoxDO: magicBoxDOList){
            magicBoxDO.setUsername(uidNicknameMap.getOrDefault(magicBoxDO.getAccountId().toString(), ""));
        }
        return magicBoxDOList;
    }

    @Override
    public List<UserPoundBoxStatisticDTO> listPoundMagicBoxStatistic(UserPoundBoxStatisticAdminQueryDTO queryDTO) {
        return userPoundBoxStatisticsDOMapper.listPoundMagicBoxStatistic(queryDTO);
    }

    @Override
    public Integer countPoundMagicBoxStatistic(UserPoundBoxStatisticAdminQueryDTO queryDTO) {
        return userPoundBoxStatisticsDOMapper.countPoundMagicBoxStatistic(queryDTO);
    }
}
